home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / util / wkdemenu.pl < prev    next >
Encoding:
Perl Script  |  1999-03-09  |  6.0 KB  |  237 lines

  1. #!/usr/bin/perl
  2. #
  3. #
  4. # kde2wmaker.pl:
  5. #
  6. #
  7. # This script, made for users of Window Maker (http://windowmaker.org) is to
  8. # be used along with KDE (http://www.kde.org).
  9. #
  10. #
  11. # The default directory, ~/.kde/share/applnk/apps, will contain various
  12. # sub-directories such as Development, Editors, Internet, etc. If for some
  13. # reason, you wish to use an alternate (parent) directory that contains the
  14. # various AppName.kdelnk files, it can be specified on the command line.
  15. #
  16. # The directory, if an alternate is specified, MUST be a parent directory to
  17. # any/all sub-directories.
  18. #
  19. # Command line usage:
  20. #    -d <KDE App.kdelnk dir> -f <output menufile> -s yes (print to STDOUT)
  21. #
  22. # Example command with args:
  23. #    -d ~/.kde/share/applnk -f ~/.kde2wmaker.menu -s yes
  24. #
  25. # When the script is run, it will write out a proper Window Maker "External
  26. # Menu" entry, that can be included in the menu. When the External Menu has
  27. # been correctly configured, the root menu will display a sub-menu containing
  28. # all of the KDE related items found. The script only needs to be run when/if
  29. # KDE is updated.
  30. #
  31. # Installation and Configuration:
  32. #
  33. # 1) If /usr/bin/perl is not the location of the perl binary on your system,
  34. #    the first line should be changed to reflect upon it's location.
  35. # 2) Run the script.
  36. # 3) Configure Window Maker's menu by editing ~/GNUstep/Defaults/WMRootMenu
  37. #    This could be done with any text editor, or by using WPrefs. Insert
  38. #    the following line (if done with a text editor) into the WMRootMenu file.
  39. #      ("External Menu", OPEN_MENU, "$HOME/.kde2wmaker.menu"),
  40. #    If done using WPrefs, simply "Add External Menu" from the drop down menu,
  41. #    then type: $HOME/.kde2wmaker.menu   into the "Menu Path/Directory List"
  42. #    textbox.
  43. # 4) Some KDE entries, such as "Pine" will require a terminal to execute it.
  44. #    There is a terminal varable below. You may use any terminal, XTerm is the
  45. #    default. Any command line options such as: -fg -bg, etc. can be
  46. #    specified in this variable as well.
  47. #
  48. #
  49. # Michael Hokenson - logan@dct.com
  50.  
  51.  
  52. ###
  53. ### Variables
  54. ###
  55.  
  56. ### The External Menu file, this should NEVER point to the root menu file
  57. $menufile = "$ENV{'HOME'}/.kde2wmaker.menu";
  58.  
  59. ### Base directory, location of all the KDE AppName.kdelnk files
  60. $basedir = "$ENV{'HOME'}/.kde/share/applnk/apps";
  61.  
  62. ### Terminal to use
  63. $term = "xterm";
  64.  
  65. ### Print to STDOUT, default is YES, a filename is specified
  66. $stdout = 1;
  67.  
  68.  
  69. ###
  70. ### Begin work
  71. ###
  72.  
  73. ### Process command line arguments
  74. foreach $arg(@ARGV) {
  75.     if($last) {
  76.         if($last eq "-d") {
  77.             $basedir = $arg;
  78.         } elsif($last eq "-f") {
  79.             $menufile = $arg;
  80.             $stdout = 0;
  81.         } 
  82.         undef($last);
  83.     } elsif($arg =~ /^-/) {
  84.         if($arg =~ /^-[dfs]$/) {
  85.             $last = $arg;
  86.         } else {
  87.             die("Unknown option: $arg\n\nUsage: kde2wmaker.pl\n\t-d <KDE App.kdelnk dir> [-f <output menufile>]\n");
  88.             &Usage;
  89.         }
  90.     }
  91. }
  92.  
  93. ### Make sure we actually exist
  94. if(-d $basedir) {
  95.  
  96.     ### Start some error checking
  97.     $errors = 0;
  98.  
  99.     ### See if there is an old menu file. If there is, rename it
  100.     unless($stdout) {
  101.         if(-e $menufile) {
  102.             print "\tFound $menufile, renaming\n\n";
  103.             rename $menufile, "$menufile.old";
  104.         }
  105.     }
  106.  
  107.     ### Read in the directories
  108.     opendir(KDE,$basedir);
  109.     @dirs = readdir(KDE);
  110.     closedir(KDE);
  111.  
  112.     ### Make sure there is actually something in $basedir
  113.     if($#dirs <= 1) {
  114.         print "ERROR:\n\tNothing found in $basedir\n\n";
  115.         exit(0);
  116.     }
  117.  
  118.     ### Begin writing the menu
  119.     unless($stdout) {
  120.         open(MENUFILE,"> $menufile");
  121.     }
  122.  
  123.     ### Start the main menu entry
  124.     if($stdout) {
  125.         print "\t\"KDE Applications\" MENU\n";
  126.     } else {
  127.         print MENUFILE "\t\"KDE Applications\" MENU\n";
  128.     }
  129.  
  130.     ### Begin processing the directories
  131.     foreach $dir(@dirs) {
  132.  
  133.         ### Handle each directory unless if its hidden (starts with .)
  134.         unless($dir =~ /^\./) {
  135.             ### Print out the sub directories
  136.             if($stdout) {
  137.                 print "\t\t\"$dir\" MENU\n";
  138.             } else {
  139.                 print MENUFILE "\t\t\"$dir\" MENU\n";
  140.             }
  141.  
  142.             ### Look in each directory and process individual files
  143.             opendir(SUB,"$basedir/$dir");
  144.             @subdirs = readdir(SUB);
  145.             closedir(SUB);
  146.  
  147.             ### Process files in each sub directory
  148.             foreach $sub(@subdirs) {
  149.  
  150.                 ### Once again, process all files but those that are hidden
  151.                 unless($sub =~ /^\./) {
  152.  
  153.                     ### Open the files
  154.                     open(SUB,"$basedir/$dir/$sub");
  155.  
  156.                     ### Search through the contents of the file
  157.                     while($line = <SUB>) {
  158.                         chop($line);
  159.                         ### Grab the name
  160.                         if($line =~ /^Name=/) {
  161.                             $pname = $line;
  162.                             $pname =~ s/Name=//;
  163.                         }
  164.                         ### Grab the command
  165.                         if($line =~ /^Exec=/) {
  166.                             $pargs = $line;
  167.                             $pargs =~ s/Exec=//;
  168.                         }
  169.                         ### If Terminal=1, then we need to execute a term
  170.                         if($line =~ /^Terminal=1$/) {
  171.                             $pargs = "$term -T \"$pname\" -e $pargs";
  172.                         }
  173.                     }
  174.  
  175.                     close(SUB);
  176.  
  177.                     ### Some error checking on the Name and Exec
  178.                     if($pname eq "") {
  179.                         $pname = $sub;
  180.                         $pname =~ s/\.kdelnk//;
  181.                     }
  182.                     if($pargs eq "") {
  183.                         $error = 1;
  184.                         $pargs = $sub;
  185.                         $pargs =~ s/\.kdelnk//;
  186.                         print "WARNING:\n\tNo Exec for $pname, using $pargs\n";
  187.                     }
  188.  
  189.                     ### Begin printing menu items
  190.                     if($stdout) {
  191.                         print "\t\t\t\"$pname\" EXEC $pargs\n";
  192.                     } else {
  193.                         print MENUFILE "\t\t\t\"$pname\" EXEC $pargs\n";
  194.                     }
  195.                 }
  196.             }
  197.  
  198.             ### Print the end of the sub menu
  199.             if($stdout) {
  200.                 print "\t\t\"$dir\" END\n";
  201.             } else {
  202.                 print MENUFILE "\t\t\"$dir\" END\n";
  203.             }
  204.         }
  205.     }
  206.  
  207.     ### Finish off the main menu entry
  208.     if($stdout) {
  209.         print "\t\"KDE Applications\" END\n";
  210.     } else {
  211.         print MENUFILE "\t\"KDE Applications\" END\n";
  212.     }
  213.  
  214.     unless($stdout) {
  215.         close(MENUFILE);
  216.     }
  217.  
  218.     ### Yaya!
  219.     if($errors) {
  220.         print "\n.. Finished. There were errors.\n";
  221.     }
  222. # else {
  223. #        print "\n.. Finished.\n";
  224. #    }
  225.  
  226.     exit(0);
  227. } else {
  228.     ### Error out :/
  229.     print "ERROR:\n\t$basedir not found\n\tTry another directory.\n";
  230.     exit(0);
  231. }
  232.  
  233. ###
  234. ### End work :))
  235. ###
  236.